#include				"SoundRotatingBuffer.h"

SoundRotatingBuffer::SoundRotatingBuffer()
{

}

SoundRotatingBuffer::~SoundRotatingBuffer()
{

}

void					SoundRotatingBuffer::init(int capacity, int tabsize)
{
	_capacity = capacity;
	for (int i = 0; i < _capacity; ++i)
	{
		_data.push_back(new short[tabsize]);
	}
	_read = _data.begin();
	_write = _data.begin();
}

void					SoundRotatingBuffer::reset()
{
	_read = _data.begin();
	_write = _data.begin();
}

short					*SoundRotatingBuffer::read()
{
	_mutex.lock();
	short				*ret = *_read;
	_read++;
	if (_read == _data.end())
		_read = _data.begin();
	_mutex.unlock();
	return ret;
}

short					*SoundRotatingBuffer::write()
{
	_mutex.lock();
	short				*ret = *_write;
	_write++;
	if (_write == _data.end())
		_write = _data.begin();
	_mutex.unlock();
	return ret;
}

bool					SoundRotatingBuffer::empty()
{
	return _read == _write;
}

int						SoundRotatingBuffer::size()
{
	return (_write - _read);
}

int						SoundRotatingBuffer::capacity()
{
	return _data.size();
}

short					*SoundRotatingBuffer::operator[](size_t size)
{
	return *(_write + size);
}